var base = null
var utils = null
var azurCtrl = null
var server = null
func _ready():
	pass
func _init():
	call_deferred("omgInit")
	pass
func _connect():
	pass

func omgInit():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		utils = globalData.infoDs["g_azurlaneUtils"]
		server = globalData.infoDs["g_azurlaneServer"]
		azurCtrl = globalData.infoDs["g_azurlane"]
		# azurCtrl.connect("resetData", self, "resetData")
		# base.connect("onNewGame",self,"gameCtrl")
		# azurCtrl.connect("onLoadGame",self,"loadGame")
		yield(sys.get_tree().create_timer(0.1), "timeout")
		test()

func test():
	if not sys.test:return
	Steam.connect("lobby_created", self, "lobbyCreated")
	Steam.connect("lobby_joined", self, "lobbyJoined")
	Steam.connect("lobby_chat_update", self, "lobbyChatUpdate")
	Steam.connect("avatar_loaded", self, "avatarLoaded")
	
	utils.createItemButton(sys.get_node("/root/Control"), self, "联机大厅", "showLobby", [], Vector2(1449, 130))

var lobbyId
func invite():
	# Steam.createLobby(0, 2)
	Steam.activateGameOverlayInviteDialog(lobbyId)
var roomWindow
var imgDic = {}
func lobbyCreated(r, id):
	lobbyId = id
	if is_instance_valid(roomWindow):
		roomWindow.queue_free()
	roomWindow = utils.createWindowDialog(sys.get_node("/root/Control"), "房间", Vector2(394, 457))
	roomWindow.popup_exclusive = true
	roomWindow.get_close_button().hide()

	var vbox = utils.createVBox(roomWindow, Vector2(30, 30))
	vbox.name = "vbox"

	var playerBox = utils.createHBox(vbox)
	Steam.getPlayerAvatar()

	var tr = TextureRect.new()
	tr.texture = ImageTexture.new()
	playerBox.add_child(tr)
	imgDic[utils.getPlayerId()] = tr

	utils.createRichTextLabel(utils.getPlayerName(), playerBox, Vector2(100, 40))

	var hbox = utils.createHBox(roomWindow, Vector2(129, 339))
	hbox.name = "hbox"

	utils.createItemButton(hbox, self, "开始", "startGame", [])
	utils.createItemButton(hbox, self, "邀请", "invite", [])

	roomWindow.popup_centered()	

func avatarLoaded(id, b, f):
	var tr = imgDic.get(id)
	var image = Image.new()
	image.load_webp_from_buffer(f)
	tr.texture.create_from_image(image)

func lobbyJoined(a, b, c, d):
	pass

func lobbyChatUpdate(lobbyId, userId, steamIDMakingChange, chatMemberStateChange):
	print(userId)

func addPlayer():
	pass

func startGame():
	pass

var lobbyWindow
func showLobby():
	if not is_instance_valid(lobbyWindow) or not lobbyWindow is WindowDialog:
		lobbyWindow = utils.createWindowDialog(sys.get_node("/root/Control"), "联机大厅", Vector2(1000, 500))
		utils.createItemButton(lobbyWindow, self, "创建", "createLobby", [], Vector2(890, 30))
		utils.createRichTextLabel("该功能尚在开发中，仅供作者调试，请无视", lobbyWindow, Vector2(500, 40), Vector2(30, 30))
	lobbyWindow.popup_centered()	

func createLobby():
	Steam.createLobby(0, 2)
